﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Logic
{
    public static class StringBuilderFunctions
    {
        public static string JoinString(params string[] words)
        {
            StringBuilder j = new StringBuilder();
            foreach (string pa in words)
            {
                j.Append(pa);
            }

            return j.ToString();
        }
        //Swaps the cases in a string
        //word -> WORD
        //Word -> wORD
        //WoRd -> wOrD
        public static string SwapCases(string input)
        {
            StringBuilder ret = new StringBuilder();
            for (int i = 0; i < input.Length; i++)
            {
                if (string.Compare(input.Substring(i, 1), input.Substring(i, 1).ToUpper(), false) == 0)
                    ret.Append(input.Substring(i, 1).ToLower());
                else
                    ret.Append(input.Substring(i, 1).ToUpper());
            }
            return ret.ToString();
        }

        //Alternates cases between letters of a string, letting the user pick if the first letter is capitalized
        public static string AlternateCases(string input, bool firstIsUpper)
        {
            StringBuilder ret = new StringBuilder();
            for (int i = 0; i < input.Length; i++)
            {
                if (firstIsUpper)
                    ret.Append(input.Substring(i, 1).ToUpper());
                else
                    ret.Append(input.Substring(i, 1).ToLower());

                firstIsUpper = !firstIsUpper;
            }

            return ret.ToString();
        }

        //Removes vowels from a word
        //remove -> rmv
        public static string RemoveVowels(string input)
        {
            StringBuilder ret = new StringBuilder();
            string currentLetter;
            for (int i = 0; i < input.Length; i++)
            {
                currentLetter = input.Substring(i, 1);

                if (string.Compare(currentLetter, "a", true) != 0 &&
                    string.Compare(currentLetter, "e", true) != 0 &&
                    string.Compare(currentLetter, "i", true) != 0 &&
                    string.Compare(currentLetter, "o", true) != 0 &&
                    string.Compare(currentLetter, "u", true) != 0)
                {
                    //Not a vowel, add it
                    ret.Append(currentLetter);
                }
            }
            return ret.ToString();
        }

        //Removes vowels from a word
        //remove -> eoe
        public static string KeepVowels(string input)
        {
            StringBuilder ret = new StringBuilder();
            string currentLetter;
            for (int i = 0; i < input.Length; i++)
            {
                currentLetter = input.Substring(i, 1);

                if (string.Compare(currentLetter, "a", true) == 0 ||
                    string.Compare(currentLetter, "e", true) == 0 ||
                    string.Compare(currentLetter, "i", true) == 0 ||
                    string.Compare(currentLetter, "o", true) == 0 ||
                    string.Compare(currentLetter, "u", true) == 0)
                {
                    //A vowel, add it
                    ret.Append(currentLetter);
                }
            }
            return ret.ToString();
        }

        //Returns an array converted into a string
        public static string ArrayToString(Array input, string separator)
        {
            StringBuilder ret = new StringBuilder();
            for (int i = 0; i < input.Length; i++)
            {
                ret.Append(input.GetValue(i).ToString());
                if (i != input.Length - 1)
                    ret.Append(separator);
            }
            return ret.ToString();
        }

        //Inserts a separator after every letter
        //hello, - -> h-e-l-l-o
        public static string InsertSeparator(string input, string separator)
        {
            StringBuilder ret = new StringBuilder();
            for (int i = 0; i < input.Length; i++)
            {
                ret.Append(input.Substring(i, 1));
                if (i != input.Length - 1)
                    ret.Append(separator);
            }
            return ret.ToString();
        }

        //Inserts a separator after every Count letters
        //hello, -, 2 -> he-ll-o
        public static string InsertSeparatorEvery(string input, string separator, int count)
        {
            StringBuilder ret = new StringBuilder();
            for (int i = 0; i < input.Length; i++)
            {
                if (i + count < input.Length)
                    ret.Append(input.Substring(i, count));
                else
                    ret.Append(input.Substring(i));

                if (i != input.Length - 1)
                    ret.Append(separator);
            }
            return ret.ToString();
        }

        //Reverses a string
        //Hello -> olleH
        public static string Reverse(string input)
        {
            StringBuilder ret = new StringBuilder();
            for (int i = input.Length - 1; i >= 0; i--)
            {
                ret.Append(input.Substring(i, 1));
            }
            return ret.ToString();
        }
    }
}
